package practica.Vista;

import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import practica.Controlador.Controlador;
import practica.modelo.casino.Casino;

	/**
	 *
	 * @author carolinamarugancruz
	 */
	public class Interfaz extends javax.swing.JFrame {

	    /** Creates new form Interfaz */
	    public Interfaz() {
	        initComponents();
	        this.jMenuItem3.setVisible(false);
	        this.jMenuItem1.setVisible(false);
	        
	    }

	    /** This method is called from within the constructor to
	     * initialize the form.
	     * WARNING: Do NOT modify this code. The content of this method is
	     * always regenerated by the Form Editor.
	     */
	    @SuppressWarnings("unchecked")
	    // <editor-fold defaultstate="collapsed" desc="Generated Code">
	    private void initComponents() {

	        jScrollPane1 = new javax.swing.JScrollPane();
	        jTextArea1 = new javax.swing.JTextArea();
	        jScrollPane2 = new javax.swing.JScrollPane();
	        jTextArea2 = new javax.swing.JTextArea();
	        jLabel1 = new javax.swing.JLabel();
	        jLabel2 = new javax.swing.JLabel();
	        jMenuBar1 = new javax.swing.JMenuBar();
	        jMenu1 = new javax.swing.JMenu();
	        jMenuItem1 = new javax.swing.JMenuItem();
	        jMenuItem2 = new javax.swing.JMenuItem();
	        jMenuItem3 = new javax.swing.JMenuItem();
	        jMenuItem4 = new javax.swing.JMenuItem();
	        jMenu2 = new javax.swing.JMenu();
	        jMenu3 = new javax.swing.JMenu();
	        jMenuItem5 = new javax.swing.JMenuItem();
	        jMenuItem6 = new javax.swing.JMenuItem();
	        jMenuItem7 = new javax.swing.JMenuItem();
	        jMenuItem8 = new javax.swing.JMenuItem();
	        jMenuItem9 = new javax.swing.JMenuItem();
	        jMenuItem10 = new javax.swing.JMenuItem();
	        jMenuItem11 = new javax.swing.JMenuItem();
	        jMenuItem12 = new javax.swing.JMenuItem();
	        jMenuItem13 = new javax.swing.JMenuItem();
	        jMenuItem14 = new javax.swing.JMenuItem();
	        jMenuItem15 = new javax.swing.JMenuItem();
	        jMenuItem16 = new javax.swing.JMenuItem();

	        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

	        jTextArea1.setColumns(20);
	        jTextArea1.setRows(5);
	        jScrollPane1.setViewportView(jTextArea1);

	        jTextArea2.setColumns(20);
	        jTextArea2.setRows(5);
	        jScrollPane2.setViewportView(jTextArea2);

	        jLabel1.setText("CASINO");

	        jLabel2.setText("MINIJUEGO");

	        jMenu1.setText("Fichero");

	        jMenuItem1.setText("Generar Casino");
	        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                jMenuItem1ActionPerformed(evt);
	            }
	        });
	        jMenu1.add(jMenuItem1);

	        jMenuItem2.setText("Cargar Casino");
	        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                jMenuItem2ActionPerformed(evt);
	            }
	        });
	        jMenu1.add(jMenuItem2);

	        jMenuItem3.setText("Salvar Casino");
	        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                jMenuItem3ActionPerformed(evt);
	            }
	        });
	        jMenu1.add(jMenuItem3);

	        jMenuItem4.setText("Salir");
	        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                jMenuItem4ActionPerformed(evt);
	            }
	        });
	        jMenu1.add(jMenuItem4);

	        jMenuBar1.add(jMenu1);

	        jMenu2.setText("Ejecutar Casino");
	        jMenu2.addMouseListener(new java.awt.event.MouseAdapter() {
	            public void mouseClicked(java.awt.event.MouseEvent evt) {
	                jMenu2MouseClicked(evt);
	            }
	        });
	        jMenuBar1.add(jMenu2);

	        jMenu3.setText("Minijuegos");

	        jMenuItem5.setText("Canibales");
	        jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                jMenuItem5ActionPerformed(evt);
	            }
	        });
	        jMenu3.add(jMenuItem5);

	        jMenuItem6.setText("Garrafas");
	        jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                jMenuItem6ActionPerformed(evt);
	            }
	        });
	        jMenu3.add(jMenuItem6);

	        jMenuItem7.setText("LoboColCabra");
	        jMenuItem7.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                jMenuItem7ActionPerformed(evt);
	            }
	        });
	        jMenu3.add(jMenuItem7);

	        jMenuItem8.setText("Puzzle");
	        jMenuItem8.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                jMenuItem8ActionPerformed(evt);
	            }
	        });
	        jMenu3.add(jMenuItem8);

	        jMenuItem9.setText("TorreHanoi");
	        jMenuItem9.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                jMenuItem9ActionPerformed(evt);
	            }
	        });
	        jMenu3.add(jMenuItem9);

	        jMenuItem10.setText("Palillos");
	        jMenuItem10.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                jMenuItem10ActionPerformed(evt);
	            }
	        });
	        jMenu3.add(jMenuItem10);

	        jMenuItem11.setText("NReinas");
	        jMenuItem11.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                jMenuItem11ActionPerformed(evt);
	            }
	        });
	        jMenu3.add(jMenuItem11);

	        jMenuItem12.setText("Laberinto");
	        jMenuItem12.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                jMenuItem12ActionPerformed(evt);
	            }
	        });
	        jMenu3.add(jMenuItem12);

	        jMenuItem13.setText("BuscaLetras");
	        jMenuItem13.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                jMenuItem13ActionPerformed(evt);
	            }
	        });
	        jMenu3.add(jMenuItem13);

	        jMenuItem14.setText("Mono");
	        jMenuItem14.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                jMenuItem14ActionPerformed(evt);
	            }
	        });
	        jMenu3.add(jMenuItem14);

	        jMenuItem15.setText("Bandera");
	        jMenuItem15.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                jMenuItem15ActionPerformed(evt);
	            }
	        });
	        jMenu3.add(jMenuItem16);

	        jMenuItem16.setText("NB");
	        jMenuItem16.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                jMenuItem16ActionPerformed(evt);
	            }

				
	        });
	        jMenu3.add(jMenuItem15);

	        jMenuBar1.add(jMenu3);

	        setJMenuBar(jMenuBar1);

	        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
	        getContentPane().setLayout(layout);
	        layout.setHorizontalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	                .addGap(138, 138, 138)
	                .addComponent(jLabel1)
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 478, Short.MAX_VALUE)
	                .addComponent(jLabel2)
	                .addGap(294, 294, 294))
	            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
	                .addGap(40, 40, 40)
	                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 402, javax.swing.GroupLayout.PREFERRED_SIZE)
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 107, Short.MAX_VALUE)
	                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 377, javax.swing.GroupLayout.PREFERRED_SIZE)
	                .addGap(101, 101, 101))
	        );
	        layout.setVerticalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	                .addGap(36, 36, 36)
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(jLabel1)
	                    .addComponent(jLabel2))
	                .addGap(18, 18, 18)
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 451, javax.swing.GroupLayout.PREFERRED_SIZE)
	                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 436, javax.swing.GroupLayout.PREFERRED_SIZE))
	                .addContainerGap(109, Short.MAX_VALUE))
	        );

	        pack();
	    }// </editor-fold>

	    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {

	        this.miControlador.generarCasinoAleatorio();
	       
	    }

	    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {
	    	JFileChooser chooser= new JFileChooser();
	    	if(chooser.showOpenDialog(this)==JFileChooser.APPROVE_OPTION)
		          this.miControlador.cargardeFichero(chooser.getSelectedFile().getAbsolutePath());
	    }

	    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {
	    	JFileChooser chooser= new JFileChooser();


		       if (chooser.showSaveDialog(new JPanel()) == JFileChooser.APPROVE_OPTION)
		       {
		          String path = (chooser.getSelectedFile().toString());

		          try {
		              if (!path.endsWith(".bas"))
		                  path = path + ".bas";
		              
		              File definitivo = new File(path);
		              definitivo.delete();
		              definitivo.createNewFile();
		              FileOutputStream fos = new FileOutputStream(definitivo);
		              int byteLeido;
		              String s = this.jTextArea1.getText();
		              for (int i = 0; i < s.length(); i++) {
		                  byteLeido = s.codePointAt(i);
		                  fos.write(byteLeido);
		              }
		              fos.close();
		              JOptionPane.showMessageDialog(null, "Archivo guardado con exito : " + path, "Inteligencia Artificial 2009/2010", JOptionPane.INFORMATION_MESSAGE);
		          } catch (Exception ex) {
		              JOptionPane.showMessageDialog(null, "Error al guardar el archivo: " + path, "Inteligencia Artificial 2009/2010", JOptionPane.ERROR_MESSAGE);
		          }

		          }

	    }

	    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {
	        System.out.println("Sin hacer");
	        System.exit(0);
	    }

	    private void jMenu2MouseClicked(java.awt.event.MouseEvent evt) {
	        
	       this.miControlador.ejecutaCasino();
	    }

	    private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {
	        System.out.println("Canibales");
	        this.miControlador.ejecutarJuego(2);
	    }

	    private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {
	        System.out.println("Garrafas");
	        this.miControlador.ejecutarJuego(3);
	        
	    }

	    private void jMenuItem7ActionPerformed(java.awt.event.ActionEvent evt) {
	        System.out.println("LoboColCabra");
	        this.miControlador.ejecutarJuego(1);
	    }

	    private void jMenuItem8ActionPerformed(java.awt.event.ActionEvent evt) {
	        System.out.println("Puzzle");
	        this.miControlador.ejecutarJuego(10);
	    }

	    private void jMenuItem9ActionPerformed(java.awt.event.ActionEvent evt) {
	        System.out.println("Hannoi");
	        this.miControlador.ejecutarJuego(9);
	    }

	    private void jMenuItem10ActionPerformed(java.awt.event.ActionEvent evt) {
	        System.out.println("Palillos");
	        this.miControlador.ejecutarJuego(8);
	    }

	    private void jMenuItem11ActionPerformed(java.awt.event.ActionEvent evt) {
	        System.out.println("NReinas");
	        this.miControlador.ejecutarJuego(5);
	    }

	    private void jMenuItem12ActionPerformed(java.awt.event.ActionEvent evt) {
	            System.out.println("Laberinto");
	            this.miControlador.ejecutarJuego(7);
	    }

	    private void jMenuItem13ActionPerformed(java.awt.event.ActionEvent evt) {
	        System.out.println("BuscaLetras");
	        this.miControlador.ejecutarJuego(12);
	    }

	    private void jMenuItem14ActionPerformed(java.awt.event.ActionEvent evt) {
	        System.out.println("Mono");
	        this.miControlador.ejecutarJuego(4);
	    }

	    private void jMenuItem15ActionPerformed(java.awt.event.ActionEvent evt) {
	        System.out.println("Bandera");
	        this.miControlador.ejecutarJuego(6);
	    }
	    
	    private void jMenuItem16ActionPerformed(java.awt.event.ActionEvent evt) {
			System.out.println("NB");
			this.miControlador.ejecutarJuego(11);
			
		}
	    public  void mostrarMinijuegoParticular(StringBuffer resultado)
	    {
	    	this.jTextArea2.setText(resultado.toString());
	    }

	    public void mostrarMinijuego(String resultado)
	    {
	    	//this.jTextArea2.setText(resultado);
	    	this.jTextArea2.append(resultado);
	    }
	    
	    
	    public void volcarVistaALog()
	    {
	    	try {
				FileWriter FicheroLog = new FileWriter("Nuevolog.txt");
				FicheroLog.write("!!!!!!!!!!!!!!!!!!!!! CASINO  !!!!!!!!!!!!!!!!!!!!!!!!!");
				FicheroLog.write("\n");
				FicheroLog.write(this.jTextArea1.getText());
				FicheroLog.write("!!!!!!!!!!!!!!!!!!!!! PASO A PASO CASINO  !!!!!!!!!!!!!!!!!!!!!!!!!");
				FicheroLog.write(this.jTextArea2.getText());
				FicheroLog.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    	
	    }
	    /**
	    * @param args the command line arguments
	    */
	    public static void main(String args[]) {
	        java.awt.EventQueue.invokeLater(new Runnable() {
	            public void run() {
	                new Interfaz().setVisible(true);
	            }
	        });
	    }

	    // Variables declaration - do not modify
	    private Controlador miControlador;
	    private javax.swing.JLabel jLabel1;
	    private javax.swing.JLabel jLabel2;
	    private javax.swing.JMenu jMenu1;
	    private javax.swing.JMenu jMenu2;
	    private javax.swing.JMenu jMenu3;
	    private javax.swing.JMenuBar jMenuBar1;
	    private javax.swing.JMenuItem jMenuItem1;
	    private javax.swing.JMenuItem jMenuItem10;
	    private javax.swing.JMenuItem jMenuItem11;
	    private javax.swing.JMenuItem jMenuItem12;
	    private javax.swing.JMenuItem jMenuItem13;
	    private javax.swing.JMenuItem jMenuItem14;
	    private javax.swing.JMenuItem jMenuItem15;
	    private javax.swing.JMenuItem jMenuItem16;
	    private javax.swing.JMenuItem jMenuItem2;
	    private javax.swing.JMenuItem jMenuItem3;
	    private javax.swing.JMenuItem jMenuItem4;
	    private javax.swing.JMenuItem jMenuItem5;
	    private javax.swing.JMenuItem jMenuItem6;
	    private javax.swing.JMenuItem jMenuItem7;
	    private javax.swing.JMenuItem jMenuItem8;
	    private javax.swing.JMenuItem jMenuItem9;
	    private javax.swing.JScrollPane jScrollPane1;
	    private javax.swing.JScrollPane jScrollPane2;
	    private javax.swing.JTextArea jTextArea1;
	    private javax.swing.JTextArea jTextArea2;
	    // End of variables declaration
		public void registerControlador(Controlador controlador) {
			this.miControlador=controlador;
			
		}

		public void mostrarCasino(String casino) {
			this.jTextArea1.append(casino);
			
		}

		public void limpiarVistaCasino() {
			this.jTextArea1.setText("");
			
		}

	}


